package com.cz.modules.gc.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.cz.commons.dynamic.datasource.annotation.DataSource;
import com.cz.modules.gc.entity.GcToiletEntity;
import com.cz.modules.gc.entity.StatoiletseatEntity;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.gc.dao.StatoiletstatusDao;
import com.cz.modules.gc.entity.StatoiletstatusEntity;
import com.cz.modules.gc.service.StatoiletstatusService;
import org.springframework.transaction.annotation.Transactional;


@Service("statoiletstatusService")
@Transactional
@DataSource("slave1")
public class StatoiletstatusServiceImpl extends ServiceImpl<StatoiletstatusDao, StatoiletstatusEntity> implements StatoiletstatusService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StatoiletstatusEntity> page = this.page(
                new Query<StatoiletstatusEntity>().getPage(params),
                new QueryWrapper<StatoiletstatusEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<StatoiletstatusEntity> list(Wrapper<StatoiletstatusEntity> queryWrapper) {
        return super.list(queryWrapper);
    }

    @Override
    public Map<String, Object> getToiletStatus() {
        Calendar c=Calendar.getInstance();
        c.add(Calendar.MINUTE,-10);
        List<StatoiletstatusEntity> statusList=this.list(
                new QueryWrapper<StatoiletstatusEntity>()
                        .ge("validdatetime",c.getTime())
                        .orderByDesc("validdatetime")
        );
        Map<String,Object> resultMap=new HashMap<>();
        for (StatoiletstatusEntity status:statusList) {
            if(!resultMap.containsKey(status.getToiletcode())){
                resultMap.put(status.getToiletcode(),status);
            }
        }
        return resultMap;
    }

    @Override
    public List<Integer> getCount(String s, String startTime, String endTime) {
        List<Integer> list=new ArrayList();

        Long start= DateUtil.parse(startTime).getTime();
        Long end=DateUtil.parse(endTime).getTime();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date(start));
        List<Long> timeList=new ArrayList<>();
        for (Long i = start; i <=end ; i=calendar.getTimeInMillis()) {
            timeList.add(calendar.getTimeInMillis());
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }
        List<String> timeStrList=new ArrayList<>();
        for (Long ltime:timeList) {
            timeStrList.add(DateUtil.format(new Date(ltime),"MM-dd"));
        }

        //该时间段内的数据

        List<StatoiletstatusEntity> gclist=this.list(
                new QueryWrapper<StatoiletstatusEntity>()
                        .eq("ToiletCode",s)
                        .ge("ValidDateTime",startTime)
                        .le("ValidDateTime",endTime)
                        .orderByDesc("ValidDateTime")
        );
        for (String time:timeStrList) {
            int count=0;
            for (StatoiletstatusEntity gc:gclist) {
                String tt=DateUtil.format(gc.getValiddatetime(),"MM-dd");
                if(tt.equals(time)){
                    count =gc.getVisitors();
                    break;
                }
            }
            list.add(count);
        }
        return list;
    }

}
